package com.lightsaber.cut.func;

import java.util.HashMap;
import java.util.Map;

/**
 * 两种料的函数，二维函数
 * 
 * @author jliu
 * 
 */
public class TwoDimentionFunc {
    /**
     * 尺寸总长
     */
    private Integer l;

    /**
     * a料长
     */
    private Integer a;

    /**
     * b料长
     */
    private Integer b;

    /**
     * 允许的最大余料
     */
    private Integer m;

    public TwoDimentionFunc(Integer l, Integer a, Integer b, Integer m) {
        super();
        this.l = l;
        this.a = a;
        this.b = b;
        this.m = m;
    }

    /**
     * y = (-a/b)*x + l/b, 的到的值向上取整
     * 
     * @param x
     * @return
     */
    public float getMinY(float x) {
        float y = Double.valueOf((-(1.0 * a) / b) * x + (1.0 * l) / b).floatValue();

        System.out.println(String.format(
                "y(min) = (-a/b)*x + l/b, (l=%d, a=%d, b=%d, m=%d, x=%f)时 y=%f", l, a, b, m, x, y));

        return y;
    }

    /**
     * y = (-a/b)*x + l/(b-m)
     * 
     * @param x
     * @return
     */
    public float getMaxY(float x) {
        float y = Double.valueOf((-(1.0 * a) / b) * x + (1.0 * l) / (b - m)).floatValue();

        System.out.println(String.format(
                "y(max) = (-a/b)*x + l/(b-m), (l=%d, a=%d, b=%d, m=%d, x=%f)时 y=%f", l, a, b, m, x,
                y));

        return y;
    }

    /**
     * x(max) = l/(a-m)
     * 
     * @param x
     * @return
     */
    public float getMaxX() {
        float x = Double.valueOf((1.0 * l) / (a - m)).floatValue();

        System.out.println(String.format("x(max) = l/(a-m), (l=%d, a=%d, m=%d)时 y=%f", l, a, m, x));

        return x;
    }

    /**
     * @param cutXMax
     *            只切割 a 得到最大x
     * @param cutYMax
     *            只切割 b 得到最大y
     * @return
     */
    public Solutions getValidPoints(float cutXMax, float cutYMax) {
        System.out.println("开始二维线性分析.....................");
        Solutions solutions = new Solutions();

        float xMax = getMaxX();
        // 分支函数, 取小
        xMax = (xMax < cutXMax ? xMax : cutXMax);

        float yMin = 0f;
        float yMax = 0f;

        for (int x = 0; x <= xMax; x++) {
            yMin = this.getMinY(x);
            yMax = this.getMaxY(x);

            // 分支函数, 取小
            yMax = (yMax < cutYMax ? yMax : cutYMax);

            for (int y = 0; y <= yMax; y++) {
                Point p = new Point(x, y);

                if (y < yMin) {// 不在线性范围内
                    solutions.solutionOfGiveUpAdd(p);
                } else {
                    solutions.solutionOfLineAnalyzeAdd(p);
                }
            }
        }

        return solutions;
    }
    

}
